package com.example.myapplication.activity;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.graphics.drawable.Drawable;

import com.example.myapplication.adpter.FragmentAdapter;
import com.example.myapplication.base.BaseActivity;
import com.example.myapplication.R;
import com.example.myapplication.fragment.HomeFragment;
import com.example.myapplication.fragment.NewsFragment;
import com.example.myapplication.fragment.PersonFragment;
import com.example.myapplication.fragment.ServiceFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private AppCompatTextView titleTextView = null;
    private String[] titleString = new String[4];
    private Drawable[] naviDrawables = new Drawable[4];
    private Drawable[] naviPressDrawables = new Drawable[4];
    private AppCompatTextView[] naviTextView = new AppCompatTextView[4];
    private ViewPager2 mainViewPager2 = null;

    @Override
    public void initView() {

        setContentView(R.layout.activity_main);
        titleTextView = findViewById(R.id.titleTextView);
        naviTextView[0] = findViewById(R.id.homeTextView);
        naviTextView[1] = findViewById(R.id.serviceTextView);
        naviTextView[2] = findViewById(R.id.newsTextView);
        naviTextView[3] = findViewById(R.id.personTextView);
        mainViewPager2 = findViewById(R.id.mainViewPager2);

    }

    @Override
    public void initData() {

        titleString[0] = "智慧城市";
        titleString[1] = "全部服务";
        titleString[2] = "新闻";
        titleString[3] = "个人中心";
        naviDrawables[0] = getDrawable(R.drawable.ic_main_navi_home);
        naviDrawables[1] = getDrawable(R.drawable.ic_main_navi_service);
        naviDrawables[2] = getDrawable(R.drawable.ic_main_navi_news);
        naviDrawables[3] = getDrawable(R.drawable.ic_main_navi_person);
        naviPressDrawables[0] = getDrawable(R.drawable.ic_main_navi_home_press);
        naviPressDrawables[1] = getDrawable(R.drawable.ic_main_navi_service_press);
        naviPressDrawables[2] = getDrawable(R.drawable.ic_main_navi_news_press);
        naviPressDrawables[3] = getDrawable(R.drawable.ic_main_navi_person_press);

        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new ServiceFragment());
        fragmentList.add(new NewsFragment());
        fragmentList.add(new PersonFragment());
        mainViewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        mainViewPager2.setAdapter(new FragmentAdapter(this, fragmentList));
        mainViewPager2.setOffscreenPageLimit(naviTextView.length);

    }

    @Override
    public void initEvent() {

        for (int i = 0; i < naviTextView.length; i++) {
            final int position = i;
            naviTextView[i].setOnClickListener(view -> {
                //把四个标签还原回默认状态
                for (int j = 0; j < naviTextView.length; j++) {
                    naviTextView[j].setCompoundDrawablesWithIntrinsicBounds(null, naviDrawables[j], null, null);
                    naviTextView[j].setTextColor(getColor(R.color.main_navi_normal));
                }
                //把选中的标签设置为选中状态
                naviTextView[position].setCompoundDrawablesWithIntrinsicBounds(null, naviPressDrawables[position], null, null);
                naviTextView[position].setTextColor(getColor(R.color.purple_500));
                titleTextView.setText(titleString[position]);
                mainViewPager2.setCurrentItem(position);
            });
        }

        mainViewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                naviTextView[position].callOnClick();
                super.onPageSelected(position);
            }
        });

    }

}
